//
//  NearbyPhotoViewController.m
//  CommonFrame
//
//  Created by Bang-guo on 15/7/23.
//  Copyright (c) 2015年 guobang. All rights reserved.
//

#import "NearbyPhotoViewController.h"

@interface NearbyPhotoViewController ()

@end

@implementation NearbyPhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.photoArray = [NSMutableArray array];
    self.navigationItem.title = @"照片墙";
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置item大小
    flowLayout.itemSize = CGSizeMake((WIDTH - 40) / 3, (WIDTH - 40) / 3);

    //设置滑动方向:水平 垂直
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //距离边界的间距 上 左 下 右
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 9, 10, 9);
    //列间距
    flowLayout.minimumInteritemSpacing = 10;
    //行间距
    flowLayout.minimumLineSpacing = 10;
    
//    if (WIDTH == 414) {
//        
//        flowLayout.itemSize = CGSizeMake((WIDTH - 40) / 3, (WIDTH - 40) / 3);
//        
//        //设置滑动方向:水平 垂直
//        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
//        //距离边界的间距 上 左 下 右
//        flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
//        //列间距
//        flowLayout.minimumInteritemSpacing = 10;
//        //行间距
//        flowLayout.minimumLineSpacing = 10;
//
//        
//    }
    
    
    self.photoCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 113) collectionViewLayout:flowLayout];
    self.photoCollectionView.delegate = self;
    self.photoCollectionView.dataSource = self;
    self.photoCollectionView.backgroundColor = [UIColor whiteColor];
    //注册方法
    [self.photoCollectionView registerClass:[NearbyPhotoCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.view addSubview:self.photoCollectionView];
    
    [self getDataValue];
    
    
    
    
    
    
    
    
    self.nextPage = 0;
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中...";
    [self.HUD show:YES];
    //下来刷新  上拉加载
    //    [self.recommendTableView.header beginRefreshing];
    [self headerRefesh];
    [self footerRefesh];
    
    
}

-(void)headerRefesh {
    self.photoCollectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.nextPage = 0;
        
        //让菊花旋转起来
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"正在加载中...";
        [self.HUD show:YES];
        [self getDataValue];
        
    }];
    
}

-(void)footerRefesh {
    
    self.photoCollectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        self.nextPage++;
        //        self.downUpdata = NO;
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"正在加载中...";
        [self.HUD show:YES];
        [self getDataValue];
        
    }];
    
}


//设置多个分区个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"row = %ld section = %ld", indexPath.row, indexPath.section);
    PhotoDetailViewController *photoDetailVC = [[PhotoDetailViewController alloc] init];
    photoDetailVC.photoArray = self.photoArray;
    photoDetailVC.intNum = indexPath.row;
    [self.navigationController pushViewController:photoDetailVC animated:YES];
    
    
}


//设置item的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.photoArray.count;
}

//布局item
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    //直接从系统的重用池里取ite
    NearbyPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    if (self.photoArray.count != 0) {
        
        NearbyPhoto *photo = self.photoArray[indexPath.row];
        
        cell.photo = photo;
        
    }
    
    
    return cell;
}


-(void)getDataValue {
    if (self.nextPage == 0) {
        
        self.photoArray = [NSMutableArray array];
    }
    NSInteger num = 21 * self.nextPage;
    NSString *str_url = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/photos/?start=%ld&count=21&gallery_mode=true", self.nearbyDetail.type, self.nearbyDetail.placeId, num];
    NSLog(@"    =  %@", str_url);
    [AFNGetDataValue getDataAFN:str_url block:^(NSMutableDictionary *dataDic) {
        
//        NSLog(@"%@", dataDic);
        NSMutableArray *array = [NSMutableArray arrayWithArray:[dataDic objectForKey:@"items"]];
        for (NSMutableDictionary *dic in array) {
            
            NearbyPhoto *photo = [[NearbyPhoto alloc] init];
            [photo setValuesForKeysWithDictionary:dic];
            
                
//                photo.name = [dic objectForKey:@"country"];
            
            
            
            [self.photoArray addObject:photo];
            
        }
        if (self.photoArray.count == 0) {
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"暂时没有图片!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
        [self.photoCollectionView reloadData];
        

        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.photoCollectionView.footer endRefreshing];
        [self.photoCollectionView.header endRefreshing];
        [self.HUD hide:YES];
        
    }];
    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
